package com.one.servlet;

import com.one.dao.impl.userDaoImpl;
import com.one.pojo.User;
import com.one.service.impl.userServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LJ
 * @create 2022-04-24-15:12
 */
@WebServlet("/login")
public class loginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user=new User();
        user.setName(username);
        user.setPassword(password);
        userServiceImpl userService = new userServiceImpl();
        boolean b = userService.userLogin(user);
        if (b){
            System.out.println("truelogin");
            HttpSession session = req.getSession();
            session.setAttribute("name", user.getName());
            session.setAttribute("id",userDaoImpl.id);
            resp.sendRedirect("index");
        }else {
            req.setAttribute("goLogin", 123);
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("index");
            requestDispatcher.forward(req, resp);
        }

    }
}
